package com.xebest.web.controller.restapi;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.PayUmApplyDTO;
import com.xebest.api.service.PayUmApplyRestApiService;
import com.xebest.api.util.OpertorLogUtil;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.enums.logtype.ManLogTypeEnum;
import com.xebest.web.form.PayUmApplyForm;
import com.xebest.web.service.PayUmApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 联动炼金相关
 *
 * @author 任建波
 */


@Controller
@RequestMapping("restapi/payUmApply")
public class PayUmApplyRestApi extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    PayUmApplyService payUmApplyService;

    /*
    获取联动链金注册信息
     */
    @RequestMapping("/getPayUmApplyList")
    @ResponseBody
    public BaseResponse getPayUmApplyList(@RequestBody PayUmApplyDTO payUmApplyDTO){
        logger.info("获取联动链金注册信息，{}", JSONObject.toJSONString(payUmApplyDTO));
        try {
            BaseResponse sendAjax = payUmApplyService.getPayUmApplyList(payUmApplyDTO);

            return sendAjax;
        }catch (IllegalArgumentException iae) {
            logger.warn("获取联动链金注接口异常：" + iae.getMessage());
            return sendAjaxError(iae.getMessage());
        }catch (Exception e){
            logger.error("获取联动链金注接口异常，{}",e);
            return sendAjaxError("操作异常");
        }
    }

    /*
   获取所有联动链金注册信息
    */
    @RequestMapping("/getPayUmApplyListByModel")
    @ResponseBody
    public List<PayUmApplyDTO> getPayUmApplyListByModel(@RequestBody PayUmApplyDTO payUmApplyDTO){
        return  payUmApplyService.getPayUmApplyListByModel(payUmApplyDTO);
    }

    /**
     * 审核联动链金的注册信息
     */
    @RequestMapping("/auditPayUmApply")
    @ResponseBody
    public BaseResponse auditPayUmApply(@RequestBody PayUmApplyDTO payUmApplyDTO){
        logger.info("审核联动链金的注册信息，{}", JSON.toJSONString(payUmApplyDTO));

        try {
            BaseResponse baseResponse = payUmApplyService.auditPayUmApply(payUmApplyDTO, payUmApplyDTO.getLoginName());

            // 添加操作日志信息
            StringBuffer content = new StringBuffer();
            content.append("审核联动链金的注册信息").append(JSONObject.toJSONString(payUmApplyDTO));
            OpertorLogUtil.opertorLog.sendOperatorLogByParam(getRequestIP(),null, ManLogTypeEnum.manPayCfca.getValue(),  payUmApplyDTO.getLoginName(), content.toString(), null);

            return baseResponse;
        }catch (IllegalArgumentException iae) {
            logger.warn("审核联动链金的注册信息异常：" + iae.getMessage());
            return sendAjaxError(iae.getMessage());
        }catch (Exception e){
            logger.error("审核联动链金的注册信息异常，{}",e);
            return sendAjaxError("操作异常");
        }
    }


    /**
     * 禁用联动链金的注册
     */
    @RequestMapping("/disablePayUmApply")
    @ResponseBody
    public BaseResponse disablePayUmApply(@RequestBody PayUmApplyDTO payUmApplyDTO){
        logger.info("禁用联动链金的注册，{}", JSON.toJSONString(payUmApplyDTO));

        try {
            //校验参数
            BaseResponse baseResponse = payUmApplyService.disablePayUmApply(payUmApplyDTO,  payUmApplyDTO.getLoginName());
            return baseResponse;
        }catch (IllegalArgumentException iae) {
            logger.warn("禁用联动链金的注册异常：" + iae.getMessage());
            return sendAjaxError(iae.getMessage());
        }catch (Exception e){
            logger.error("禁用联动链金的注册异常，{}",e);
            return sendAjaxError("操作异常");
        }
    }

}
